package com.example.hotelmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelmanager.entity.BookMsg;
import com.example.hotelmanager.entity.CheckIn;
import com.example.hotelmanager.entity.Guest;
import com.example.hotelmanager.entity.vo.GuestMsg;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.mapper.GuestMapper;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :chenjie
 * @date :Created 2022/11/27 23:00
 */
@Slf4j
@Service
public class GuestServiceImpl extends ServiceImpl<GuestMapper, Guest>implements GuestService {
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private CostService costService;
    @Override
    public boolean deleteGuest(String idCard) {
            LambdaQueryWrapper<BookMsg> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(BookMsg::getGuestIdCard,idCard);
            bookMsgService.remove(queryWrapper);
            LambdaQueryWrapper<CheckIn> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(CheckIn::getGuestIdCard,idCard);
            List<CheckIn> checkIns = checkInService.list(queryWrapper1);
            if(checkIns != null && checkIns.size()>0){
                ArrayList<String> list=new ArrayList<>();
                for(CheckIn checkIn:checkIns){
                    list.add(checkIn.getRoomId());
                }
                costService.removeByIds(list);
            }
            LambdaQueryWrapper<CheckIn> queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(CheckIn::getGuestIdCard,idCard);
            checkInService.remove(queryWrapper2);
            this.baseMapper.deleteById(idCard);
            return true;

    }

    /**
     * 身份证
     * @param idCard
     * @return
     */
    @Override
    public List<GuestMsg> getGuestByIdCard(String idCard) {
        LambdaQueryWrapper<Guest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Guest::getIdCard,idCard);
        Guest guests = this.baseMapper.selectOne(queryWrapper);
        List<GuestMsg> guestMsgs = new ArrayList<>();
        if(guests != null) {
            LambdaQueryWrapper<BookMsg> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(BookMsg::getGuestIdCard,idCard);
            List<BookMsg> bookMsgs = bookMsgService.list(queryWrapper1);
            LambdaQueryWrapper<CheckIn>queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(CheckIn::getGuestIdCard,idCard);
            List<CheckIn> checkIns = checkInService.list(queryWrapper2);
            //根据订单信息设置客户状态
            GuestMsg guestMsg = new GuestMsg(guests.getIdCard(), guests.getName(), guests.getContact(), -1, "");
            if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 1) {
                guestMsg.setState(1);
                guestMsg.setRoomId(checkIns.get(0).getRoomId());
            } else if (bookMsgs != null && bookMsgs.size() > 0 && bookMsgs.get(0).getState() != 11) {
                guestMsg.setState(0);
                guestMsg.setRoomId(bookMsgs.get(0).getResultRoom());
            } else if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 0) {
                guestMsg.setState(-1);
                guestMsg.setRoomId(checkIns.get(0).getRoomId());
            }
            guestMsgs.add(guestMsg);
            return guestMsgs;
        }
        return null;

    }

    @Override
    public List<GuestMsg> getGuestByContact(String contact) {
        LambdaQueryWrapper<Guest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Guest::getContact,contact);
        List<Guest> guests = this.baseMapper.selectList(queryWrapper);//防止多人公用一个号码
        List<GuestMsg> guestMsgs = new ArrayList<>();
        if(guests != null ) {

            for (Guest guest : guests) {
                LambdaQueryWrapper<BookMsg> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(BookMsg::getGuestIdCard, guest.getIdCard());
                List<BookMsg> bookMsgs = bookMsgService.list(queryWrapper1);
                LambdaQueryWrapper<CheckIn> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(CheckIn::getGuestIdCard, guest.getIdCard());
                List<CheckIn> checkIns = checkInService.list(queryWrapper2);
                GuestMsg guestMsg = new GuestMsg(guest.getIdCard(), guest.getName(), guest.getContact(), -1, "");
                if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 1) {
                    guestMsg.setState(1);
                    guestMsg.setRoomId(checkIns.get(0).getRoomId());
                } else if (bookMsgs != null && bookMsgs.size() > 0 && bookMsgs.get(0).getState() != 11) {
                    guestMsg.setState(0);
                    guestMsg.setRoomId(bookMsgs.get(0).getResultRoom());
                } else if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 0) {
                    guestMsg.setState(-1);
                    guestMsg.setRoomId(checkIns.get(0).getRoomId());
                }
                guestMsgs.add(guestMsg);

            }return guestMsgs;
        }
        return null;
    }

    @Override
    public List<GuestMsg> getGuestByName(String name) {
        LambdaQueryWrapper<Guest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Guest::getName,name);
        List<Guest> guests = this.baseMapper.selectList(queryWrapper);//重名的可能性
        List<GuestMsg> guestMsgs = new ArrayList<>();
        if(guests != null && guests.size()>0) {
            for (Guest guest : guests) {
                LambdaQueryWrapper<BookMsg> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(BookMsg::getGuestIdCard, guest.getIdCard());
                List<BookMsg> bookMsgs = bookMsgService.list(queryWrapper1);
                LambdaQueryWrapper<CheckIn> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(CheckIn::getGuestIdCard, guest.getIdCard());
                List<CheckIn> checkIns = checkInService.list(queryWrapper2);
                GuestMsg guestMsg = new GuestMsg(guest.getIdCard(), guest.getName(), guest.getContact(), -1, "");
                if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 1) {
                    guestMsg.setState(1);
                    guestMsg.setRoomId(checkIns.get(0).getRoomId());
                } else if (bookMsgs != null && bookMsgs.size() > 0 && bookMsgs.get(0).getState() != 11) {
                    guestMsg.setState(0);
                    guestMsg.setRoomId(bookMsgs.get(0).getResultRoom());
                } else if (checkIns != null && checkIns.size() > 0 && checkIns.get(0).getState() == 0) {
                    guestMsg.setState(-1);
                    guestMsg.setRoomId(checkIns.get(0).getRoomId());
                }
                guestMsgs.add(guestMsg);
            }
            return guestMsgs;
        }
        return null;
    }
}
